Expand description
This library provides wrapper types that permit sending non Send
types to
other threads and use runtime checks to ensure safety.
The types provided by this crate wrap a value and provide a Send
bound. None of
the types permit access to the enclosed value unless the thread that wrapped the
value is attempting to access it.
A Fragile
will actually send the T
from thread to thread but will only
permit the original thread to invoke the destructor. If the value gets dropped
in a different thread, the destructor will panic.
Warning: The 1.2 release family of fragile have broken Sticky
and SemiSticky
types. Because the behavior requires an API change it can only be corrected by
a major semver update.
Example
use std::thread;
use fragile::Fragile;
// creating and using a fragile object in the same thread works
let val = Fragile::new(true);
assert_eq!(*val.get(), true);
assert!(val.try_get().is_ok());
// once send to another thread it stops working
thread::spawn(move || {
assert!(val.try_get().is_err());
}).join()
.unwrap();
Why?
Most of the time trying to use this crate is going to indicate some code smell. But
there are situations where this is useful. For instance you might have a bunch of
non Send
types but want to work with a Send
error type. In that case the non
sendable extra information can be contained within the error and in cases where the
error did not cross a thread boundary yet extra information can be obtained.
Drop / Cleanup Behavior
All types will try to eagerly drop a value if they are dropped on the right thread.
Sticky
and SemiSticky
will however permanently leak memory until the program
shuts down if the value is dropped on the wrong thread. This limitation does not
exist on the 2.x release family of fragile.
Features
By default the crate has no dependencies. Optionally the slab
feature can
be enabled which optimizes the internal storage of the Sticky
type to
make it use a slab
instead.
Structs
Fragile<T>
wraps a non sendable T
to be safely send to other threads.